package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * <电视台 - 如东电视台> 解析类
 *
 * @author KK
 *
 */
public class RdxmtAnalyse implements AnalyseNews {



	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://vblog.rdxmt.com/video_play.php\\?id=[0-9]*",
				"http://www.rdxmt.com/folder3/boke/folder(?!538|539)[0-9]*/[0-9]{4}/[0-9]{2}/[0-9]{4}-[0-9]{2}-[0-9]*.html",
				"http://www.rdxmt.com/folder3/folder(663|664)/[a-z0-9]*/[0-9]{4}/[0-9]{2}/[0-9]{4}-[0-9]{2}-[0-9]*.html",
				"http://www.rdxmt.com/folder[0-9]{3}/folder[0-9]{3}/[0-9\\/\\-]+.html"
				};

		return RegexUtils.matchAny(url,regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		Long date = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		String updateUrl = null;
		String desc = null;
		Document doc = Jsoup.parse(htmltxt);

		title =   doc.select("div#liv_print_area > ul > li:eq(0)").text();
		dateStr = doc.select("div#liv_print_area > ul > li").text();
		updateUrl = doc.select("div#liv_print_area > ul > li > script").attr("src");


		if (StringUtils.isNullOrEmpty(title)){
			title =   doc.select("span#vedio_name_container").text();
			dateStr = doc.select("div.garea > div.gright > div.gsub5 > div.img_txt > ul.img_t").text();
		    clickNum = doc.select("div.gareas > div.gtop > div.rc > span.rsubcolor").text();

		}
		date = DateUtils.matchDate(dateStr);
		desc =      doc.select("li#contentarea").text();
			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(2);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(desc));
			news.setDate(date);
			return news;

	}

	
	public NewsMeta Update(NewsMeta meta) {
		if (StringUtils.isNotBlank(meta.getUpdateUrl())) {
			String html = CrawlHTML.GetHtml(meta.getUpdateUrl(), "UTF-8");
			if(StringUtils.isNotBlank(html)){
				String clickNumStr = StringUtils.substringBetween(html, "document.write('","');") ;
				Integer clickNum = NumberUtils.getInteger(clickNumStr);
				meta.setClickNum(clickNum);
				return meta;
			}
		}
		return null;
	}

	

	
}
